package com.common.base.adapter

import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.common.base.app.extras.yes

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/19
 * <p>
 * 描述：给ViewPager创建Fragment使用
 * <p>
 * 修订历史：
 */
class BaseFragmentPageAdapter : FragmentStatePagerAdapter {

    private var mFragments = arrayListOf<Fragment>()
    private var mTitles = arrayListOf<String>()
    private var fragmentManager: FragmentManager? = null

    //不被销毁页面下标列表
    private var noDestroyList = ArrayList<Int>()

    constructor(manager: FragmentManager) : super(manager) {
        this.fragmentManager = manager
    }

    constructor(manager: FragmentManager, fragments: ArrayList<Fragment>) : super(manager) {
        this.mFragments = fragments
        this.fragmentManager = manager
    }

    constructor(manager: FragmentManager, fragments: Array<Fragment>) : super(manager) {
        this.mFragments.addAll(fragments.toList())
        this.fragmentManager = manager
    }

    constructor(
        manager: FragmentManager,
        fragments: ArrayList<Fragment>,
        titles: ArrayList<String>
    ) : super(manager) {
        this.mFragments = fragments
        this.mTitles = titles
        this.fragmentManager = manager
    }


    constructor(
        manager: FragmentManager,
        fragments: ArrayList<Fragment>,
        titles: Array<String>
    ) : super(manager) {
        this.mFragments = fragments
        this.mTitles.addAll(titles.toList())
        this.fragmentManager = manager
    }

    constructor(
        manager: FragmentManager,
        fragments: Array<Fragment>,
        titles: Array<String>
    ) : super(manager) {
        this.mFragments.addAll(fragments.toList())
        this.mTitles.addAll(titles.toList())
        this.fragmentManager = manager
    }

    override fun getItem(position: Int): Fragment {
        return mFragments[position]
    }

    override fun getCount(): Int {
        return mFragments.size
    }


    override fun getPageTitle(position: Int): CharSequence? {
        return if (mTitles.isNotEmpty() && position < mTitles.size) {
            mTitles[position]
        } else {
            super.getPageTitle(position)
        }
    }

    //设置某页面不被销毁
    fun setNoDestroy(vararg position: Int) {
        noDestroyList.clear()
        noDestroyList.addAll(position.toList())
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        (noDestroyList.contains(position)).yes {
            return
        }
        super.destroyItem(container, position, `object`)
    }

    /**
     * 更新数据
     */
    fun setData(fragments: ArrayList<Fragment>, titles: ArrayList<String>) {
        mFragments = fragments
        mTitles = titles
        notifyDataSetChanged()
    }

    /**
     * 删除内存中所有缓存的fragment,以防oom
     */
    fun removeAllFragments() {
        if (!fragmentManager?.fragments.isNullOrEmpty()) {
            val ft = fragmentManager?.beginTransaction()
            val fragmentList = fragmentManager?.fragments
            fragmentList?.forEach {
                if (it != null) {
                    ft?.remove(it)
                }
            }
            ft?.commitAllowingStateLoss()
        }
    }
}